package timing.ukulele.android.fragment;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.fragment.app.Fragment;
import androidx.navigation.fragment.NavHostFragment;

import com.inuker.bluetooth.library.connect.listener.BluetoothStateListener;
import com.inuker.bluetooth.library.search.SearchRequest;
import com.inuker.bluetooth.library.search.SearchResult;
import com.inuker.bluetooth.library.search.response.SearchResponse;
import com.inuker.bluetooth.library.utils.BluetoothLog;

import java.util.ArrayList;
import java.util.List;

import timing.ukulele.android.R;
import timing.ukulele.android.bluetooth.ClientManager;
import timing.ukulele.android.bluetooth.DeviceListAdapter;
import timing.ukulele.android.bluetooth.view.PullRefreshListView;
import timing.ukulele.android.bluetooth.view.PullToRefreshFrameLayout;
import timing.ukulele.android.bluetooth.view.ViewConstants;

public class BluetoothFragment extends Fragment {

    private PullToRefreshFrameLayout mRefreshLayout;
    private PullRefreshListView mListView;
    private DeviceListAdapter mAdapter;
    private TextView mTvTitle;

    private List<SearchResult> mDevices;

    @Override
    public View onCreateView(
            LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState
    ) {
        // Inflate the layout for this fragment
        return inflater.inflate(R.layout.fragment_bluetooth, container, false);
    }

    public void onViewCreated(@NonNull View view, Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);


        mDevices = new ArrayList<>();

        mTvTitle = view.findViewById(R.id.title);

        mRefreshLayout = view.findViewById(R.id.pulllayout);

        mListView = mRefreshLayout.getPullToRefreshListView();
        mAdapter = new DeviceListAdapter(getActivity());
        mListView.setAdapter(mAdapter);

        mListView.setOnRefreshListener(this::searchDevice);

        searchDevice();

        ClientManager.getClient().registerBluetoothStateListener(new BluetoothStateListener() {
            @Override
            public void onBluetoothStateChanged(boolean openOrClosed) {
                BluetoothLog.v(String.format("onBluetoothStateChanged %b", openOrClosed));
            }
        });
    }

    private void searchDevice() {
        SearchRequest request = new SearchRequest.Builder()
                .searchBluetoothLeDevice(5000, 2).build();

        ClientManager.getClient().search(request, mSearchResponse);
    }

    private final SearchResponse mSearchResponse = new SearchResponse() {
        @Override
        public void onSearchStarted() {
            BluetoothLog.w("BluetoothFragment.onSearchStarted");
            mListView.onRefreshComplete(true);
            mRefreshLayout.showState(ViewConstants.LIST);
            mTvTitle.setText(R.string.string_refreshing);
            mDevices.clear();
        }

        @Override
        public void onDeviceFounded(SearchResult device) {
            if (!mDevices.contains(device)) {
                mDevices.add(device);
                mAdapter.setDataList(mDevices);
            }

            if (mDevices.size() > 0) {
                mRefreshLayout.showState(ViewConstants.LIST);
            }
        }

        @Override
        public void onSearchStopped() {
            BluetoothLog.w("BluetoothFragment.onSearchStopped");
            mListView.onRefreshComplete(true);
            mRefreshLayout.showState(ViewConstants.LIST);

            mTvTitle.setText(R.string.devices);
        }

        @Override
        public void onSearchCanceled() {
            BluetoothLog.w("BluetoothFragment.onSearchCanceled");

            mListView.onRefreshComplete(true);
            mRefreshLayout.showState(ViewConstants.LIST);

            mTvTitle.setText(R.string.devices);
        }
    };

    @Override
    public void onPause() {
        super.onPause();
        ClientManager.getClient().stopSearch();
    }
}